<?php declare(strict_types=1);

namespace App\Http\Requests;

use App\Exceptions\ApiException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;

abstract class BaseRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 重写表单异常返回
     * @param Validator $validator
     * @return void
     * @throws ApiException
     */
    protected function failedValidation(Validator $validator)
    {
        throw new ApiException($validator->errors()->first(),JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
    }
}
